﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.BaoCao.BCBanHangTheoNhanVien
{
    public partial class frmBanHangTheoNhanVien : Form
    {
        #region khởi tạo biến
        bool checkClose = false;
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        Entities.NhanVien[] nhanvien;
        Entities.HDBanBuon[] hdbanbuon;
        Entities.HDBanLe[] hdbanle;
        Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        Entities.ChiTietHDBanle[] chitiethdbanle;
        Entities.KhoHang[] khohang;
        Entities.DiaDiem[] diadiem;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        Entities.BCDoanhThuTheoNhanVien[] hienthi;
        Entities.BCDoanhThuTheoNhanVien[] baocao;
        Entities.BCDoanhThuTheoNhanVien save;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        #endregion

        public frmBanHangTheoNhanVien()
        {
            InitializeComponent();
            try
            {
                try
                {
                    Entities.CaiDatKho kho = Entitystatic.ViewFile();
                    makho = kho.MaKho;
                    tenkho = kho.TenKho;
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        makho = tenkho = "";
                    }
                }
                catch
                {
                }
                date =Entitystatic.date;
                batdau = ketthuc = date;
                SelectData();
                cbbKho.Text = tenkho;
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            {
            }
        }

        private void frmBanHangTheoNhanVien_Load(object sender, EventArgs e)
        {
            XuLy();
        }

        private void fix()
        {

            try
            {
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.ReadOnly = true;
                dgvHienThi.Columns["MaNV"].Visible = true;
                dgvHienThi.Columns["TenNV"].Visible = true;
                dgvHienThi.Columns["DienThoai"].Visible = true;
                dgvHienThi.Columns["DiaChi"].Visible = true;
                dgvHienThi.Columns["GiaTriDoanhThu"].Visible = true;
                dgvHienThi.Columns["GiaTriSoTienDaThu"].Visible = true;
                dgvHienThi.Columns["GiaTriSoTienPhaiThu"].Visible = true;
                dgvHienThi.Columns["MaNV"].HeaderText = "Mã Nhân Viên";
                dgvHienThi.Columns["TenNV"].HeaderText = "Tên Nhân Viên";
                dgvHienThi.Columns["DienThoai"].HeaderText = "Điện Thoại";
                dgvHienThi.Columns["DiaChi"].HeaderText = "Địa Chỉ";
                dgvHienThi.Columns["GiaTriDoanhThu"].HeaderText = "Doanh Thu";
                dgvHienThi.Columns["GiaTriSoTienDaThu"].HeaderText = "Số Tiền Đã Thu";
                dgvHienThi.Columns["GiaTriSoTienPhaiThu"].HeaderText = "Số Tiền Phải Thu";
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dgvHienThi.AllowUserToAddRows = false;
                dgvHienThi.AllowUserToDeleteRows = false;
                dgvHienThi.AllowUserToResizeRows = false;
                dgvHienThi.RowHeadersVisible = false;

            }
            catch
            {
            }
        }

        void SelectData()
        {
            Entities.NhanVien nv = new Entities.NhanVien();
            nv.HanhDong = "1";
            nv.MaNhanVien = "";
            nhanvien = Common.StaticVariable.InterfaceObject.SelectNhanVien(nv);
            if (nhanvien == null)
                nhanvien = new Entities.NhanVien[0];

            hdbanbuon = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
            if (hdbanbuon == null)
                hdbanbuon = new Entities.HDBanBuon[0];
            hdbanle = Common.StaticVariable.InterfaceObject.SelectHDBanLe();
            if (hdbanle == null)
                hdbanle = new Entities.HDBanLe[0];
            Entities.KhoHang kh = new Entities.KhoHang();
            kh.HanhDong = "1";
            kh.MaKho = "";
            khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(kh);
            if (khohang == null)
                khohang = new Entities.KhoHang[0];

            Entities.KhoHang[] temp = new Entities.KhoHang[khohang.Length + 1];
            if (temp.Length != 1)
            {
                int sotang = 0;
                foreach (Entities.KhoHang item in khohang)
                {
                    temp[sotang] = item;
                    sotang++;
                }
                temp[temp.Length - 1] = new Entities.KhoHang();
                temp[temp.Length - 1].MaKho = "Tất cả";
                temp[temp.Length - 1].TenKho = "Tất cả";
                Common.Utilities.XuLyComboBox(cbbKho, temp, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
            else
            {
                Common.Utilities.XuLyComboBox(cbbKho, khohang, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
        }

        #region In
        void In()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                Entities.BCDoanhThuTheoNhanVien[] temp = new Entities.BCDoanhThuTheoNhanVien[hienthi.Length - 1];
                int sotang = 0;
                for (int j = 0; j < hienthi.Length - 1; j++)
                {
                    temp[sotang] = hienthi[j];
                    sotang++;
                }
                BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                    frm.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý
        void XuLy()
        {
            try
            {
                string madiadiem = "";
                if (makho == "")
                {
                    
                    return;
                }
                double sotiendathu = 0;
                double sotienphaithu = 0;
                double tongtien = 0;
                int sotang = 0;
                    madiadiem = "TatCa";

                array = new ArrayList();
                foreach (Entities.NhanVien item in nhanvien)
                {
                    if (madiadiem == "TatCa")
                    {
                        double sotiendathu1, sotienphaithu1, tongtien1;
                        sotienphaithu1 = sotiendathu1 = tongtien1 = 0;
                        foreach (Entities.HDBanLe item1 in hdbanle)
                        {
                            if (cbbKho.SelectedIndex == khohang.Length)
                            {
                                if (item.MaNhanVien == item1.MaNhanVien && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                                {
                                    sotiendathu1 += double.Parse(item1.TongTienThanhToan);
                                    tongtien1 += double.Parse(item1.TongTienThanhToan);
                                    sotiendathu += double.Parse(item1.TongTienThanhToan);
                                    tongtien += double.Parse(item1.TongTienThanhToan);
                                }
                            }
                            else
                            {
                                if (item.MaNhanVien == item1.MaNhanVien && item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                                {
                                    sotiendathu1 += double.Parse(item1.TongTienThanhToan);
                                    tongtien1 += double.Parse(item1.TongTienThanhToan);
                                    sotiendathu += double.Parse(item1.TongTienThanhToan);
                                    tongtien += double.Parse(item1.TongTienThanhToan);
                                }
                            }
                        }

                        foreach (Entities.HDBanBuon item1 in hdbanbuon)
                        {
                            if (cbbKho.SelectedIndex == khohang.Length)
                            {
                                if (item.MaNhanVien == item1.MaNhanVien && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                                {
                                    sotiendathu1 += double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu1 += double.Parse(item1.TongTienThanhToan) - (double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu));
                                    tongtien1 += double.Parse(item1.TongTienThanhToan);
                                    sotiendathu += double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu += double.Parse(item1.TongTienThanhToan) - (double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu));
                                    tongtien += double.Parse(item1.TongTienThanhToan);
                                }
                            }
                            else
                            {
                                if (item.MaNhanVien == item1.MaNhanVien && item1.MaKho == makho && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc)
                                {
                                    sotiendathu1 += double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu1 += double.Parse(item1.TongTienThanhToan) - (double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu));
                                    tongtien1 += double.Parse(item1.TongTienThanhToan);
                                    sotiendathu += double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu);
                                    sotienphaithu += double.Parse(item1.TongTienThanhToan) - (double.Parse(item1.ThanhToanNgay) + double.Parse(item1.ThanhToanSauKhiLapPhieu));
                                    tongtien += double.Parse(item1.TongTienThanhToan);
                                }
                            }
                        }
                        Entities.BCDoanhThuTheoNhanVien temp = new Entities.BCDoanhThuTheoNhanVien();
                        temp.MaNV = item.MaNhanVien;
                        temp.DiaChi = "";
                        temp.DienThoai = item.DienThoaiDD;
                        temp.STT = sotang + 1;
                        temp.TenNV = item.TenNhanVien;
                        temp.DoanhThu = tongtien1;
                        temp.SoTienDaThu = sotiendathu1;
                        temp.SoTienPhaiThu = sotienphaithu1;
                        temp.GiaTriDoanhThu = Common.Utilities.FormatMoney(tongtien1.ToString());
                        temp.GiaTriSoTienDaThu = Common.Utilities.FormatMoney(sotiendathu1.ToString());
                        temp.GiaTriSoTienPhaiThu = Common.Utilities.FormatMoney(sotienphaithu1.ToString());
                        array.Add(temp);
                        sotang++;
                    }
                }
                hienthi = new Entities.BCDoanhThuTheoNhanVien[array.Count + 1];
                sotang = 0;

                foreach (Entities.BCDoanhThuTheoNhanVien item in (Entities.BCDoanhThuTheoNhanVien[])array.ToArray(typeof(Entities.BCDoanhThuTheoNhanVien)))
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
                hienthi[hienthi.Length - 1] = new Entities.BCDoanhThuTheoNhanVien();
                hienthi[hienthi.Length - 1].DiaChi = "Tổng Cộng";
                hienthi[hienthi.Length - 1].GiaTriDoanhThu = Common.Utilities.FormatMoney(tongtien.ToString());
                hienthi[hienthi.Length - 1].GiaTriSoTienDaThu = Common.Utilities.FormatMoney(sotiendathu.ToString());
                hienthi[hienthi.Length - 1].GiaTriSoTienPhaiThu = Common.Utilities.FormatMoney(sotienphaithu.ToString());
                dgvHienThi.DataSource =  hienthi; fix();
                save = null;
            }
            catch
            {
            }
        }
        #endregion

        #region Lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCDoanhThuTheoNhanVien[] temp = new Entities.BCDoanhThuTheoNhanVien[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCDoanhThuTheoNhanVien[] temp = new Entities.BCDoanhThuTheoNhanVien[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCDoanhThuTheoNhanVien[] temp = new Entities.BCDoanhThuTheoNhanVien[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void frmBanHangTheoNhanVien_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
            }
            catch
            {
            }
        }

        private void cbbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbKho.SelectedIndex < 0)
                return;
            makho = cbbKho.SelectedValue.ToString();
            tenkho = cbbKho.Text;
            XuLy();
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            In();
        }

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                i = e.RowIndex;
                save = ((Entities.BCDoanhThuTheoNhanVien[])dgvHienThi.DataSource)[i];
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            PDF();
        }

        private void panel_Button_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
    }
}
